/*
 * Created on 2005-4-30
 *
 * Copyright: Bluejay Networks Inc. 2005
 *
 * This software is the proprietary information of Bluejay Networks Inc. 
 * 
 * Use is subject to license terms.
 * 
 */
package com.ims.iaa.html;

import java.net.URL;
import java.util.LinkedList;

import com.ims.iaa.IaaThread;
import com.ims.iaa.RobotsTXTAnalyzer;
import com.ims.iaa.SpiderDataStream;
import com.ims.iaa.AD.AdDataLink;

/**
 * This class implement ImpAnalyzer interface to simulate a AdDataLink queue,
 * @see ImpAnalyzer
 */
public class ImpAnalyzerImpl implements ImpAnalyzer{
	private SpiderDataStream m_dataStream;
	private LinkedList m_AdList = new LinkedList();
	private boolean m_isOver = false;
	private String m_sUserAgent = "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT 4.0)";
	private int m_iImpDepth = -1;
	private RobotsTXTAnalyzer m_robotsTxtAna = null;
	
	void setSpiderDataStream(SpiderDataStream stream){
		//prepair for new analyse
		m_isOver = false;
		m_dataStream = stream;
	}
	public SpiderDataStream getSpiderDataStream(){
		return m_dataStream;
	}
	/**
	 * Get the AdDataLink count in queue
	 * @return
	 */
	public synchronized int getAdCount(){
		return m_AdList.size();
	}
	
	/**
	 * get the AdDataLink object to analyze from queue,
	 * then delete it form queue.
	 *  
	 * @return AdDataLink object.
	 */ 
	public synchronized AdDataLink getAdDataLink(){
		AdDataLink ad = null;
		if(m_AdList.size() > 0){
			ad = (AdDataLink)m_AdList.get(0);
			m_AdList.removeFirst();
		}
		return ad;
	}

	/* (non-Javadoc)
	 * @see com.ims.iaa.ImpAnalyzer#addAdDataLink(com.ims.iaa.AdDataLink)
	 */
	public synchronized void addAdDataLink(AdDataLink dataLink) {
		dataLink.setUserAgent(m_sUserAgent);
		m_AdList.add(dataLink);
	}

	/* (non-Javadoc)
	 * @see com.ims.iaa.ImpAnalyzer#analyzeOver()
	 */
	public synchronized void analyzeOver() {
		m_isOver = true;
	}
	
	public boolean isAnalyzeOver(){
		return m_isOver;
	}
	/* (non-Javadoc)
	 * @see com.ims.iaa.ImpAnalyzer#setUserAgent()
	 */
	public void setUserAgent(String sAgent) {
		m_sUserAgent = sAgent;
	}
    /* (non-Javadoc)
     * @see com.ims.iaa.html.ImpAnalyzer#getUserAgent()
     */
    public String getUserAgent() {
        return m_sUserAgent;
    }
    /* (non-Javadoc)
     * @see com.ims.iaa.html.ImpAnalyzer#setImpDepth(int)
     */
    public void setImpDepth(int iDepth) {
        m_iImpDepth = iDepth;
        IaaThread.setImpDepth(iDepth);
    }
    /* (non-Javadoc)
     * @see com.ims.iaa.html.ImpAnalyzer#getImpDepth()
     */
    public int getImpDepth() {
        return m_iImpDepth;
    }
    /* (non-Javadoc)
     * @see com.ims.iaa.html.ImpAnalyzer#isDisallow(java.net.URL)
     */
    public boolean isDisallow(URL url) {
        if(m_robotsTxtAna != null){
            return !m_robotsTxtAna.isAllow(url);
        }
        return false;
    }
    /* (non-Javadoc)
     * @see com.ims.iaa.html.ImpAnalyzer#setRobotsTxtAnaluzer(com.ims.iaa.RobotsTXTAnalyzer)
     */
    public void setRobotsTxtAnaluzer(RobotsTXTAnalyzer ana) {
        m_robotsTxtAna = ana;
    }
    
    ImpAnalyzerCtrl m_analyzerCtrl = null ;
    
 //++changsheng
    /* (non-Javadoc)
     * @see com.ims.iaa.html.ImpAnalyzer#getAnalyzerCtrl()
     */
    public ImpAnalyzerCtrl getAnalyzerCtrl() {
        return m_analyzerCtrl ;
    }
    /* (non-Javadoc)
     * @see com.ims.iaa.html.ImpAnalyzer#setAnalyzerCtrl()
     */
    public void setAnalyzerCtrl(ImpAnalyzerCtrl analyzerCtrl) {
        this.m_analyzerCtrl = analyzerCtrl ;
    }
 //--
    public void resetAdList(){
        m_AdList = new LinkedList();
    }
}
